package com.example.timeplanning_teacher;

import androidx.appcompat.app.AppCompatActivity;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

public class RegisterActivity extends AppCompatActivity {

    //账号
    private EditText et_password;//密码
    private EditText et_confirm_psd;
    private TextView tx_head;
    private Handler handler;
    private String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_register);

        et_password = findViewById(R.id.et_psd);
        et_confirm_psd = findViewById(R.id.et_confirmpsd);

        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("Message");
        username = bundle.getString("phnum");

        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 1:
                        Intent intent=new Intent(RegisterActivity.this, MainActivity.class);
                        startActivity(intent);
                        RegisterActivity.this.finish();
                }
            };
        };

        tx_head = findViewById(R.id.tx_head);
        Typeface textFont1 = Typeface.createFromAsset(getAssets(), "fonts/head_font.ttf");
        tx_head.setTypeface(textFont1);
    }
    public void signup(View v){
        String password=et_password.getText().toString();//获取输入的密码
        String confirm_password=et_confirm_psd.getText().toString();//获取输入的确认密码
        if(!"".equals(username) && !"".equals(password)){
                    //判断两次输入的密码是否一致
                if (!password.equals(confirm_password)) {
                    Toast.makeText(RegisterActivity.this, "两次输入的密码不一致，请重新输入！", Toast.LENGTH_SHORT).show();
                    et_confirm_psd.setText("");//清空“确认密码”编辑框
                    et_confirm_psd.requestFocus(); //让“密码”编辑框获得焦点
                } else {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                String json = "{\n" +
                                        "  \"username\": \"" + username + "\",\n" +
                                        "  \"password\": \"" + password + "\"\n" +
                                        "}";
                                OkHttpClient client = new OkHttpClient();
                                Request request = new Request.Builder()
                                        .url("http://58.58.36.198:9900/parent/register")
                                        .post(RequestBody.create(MediaType.parse("application/json"), json))
                                        .build();
                                Response response = client.newCall(request).execute();
                                String responseData = response.body().string();
                                JSONObject jsonObject = new JSONObject(responseData);
                                int code = (int) jsonObject.get("code");
                                if (code == 100) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Message message = new Message();
                                            message.what = 1;
                                            handler.sendMessage(message);
                                            Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                } else if (0 == code) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(RegisterActivity.this, "改手机号已被注册", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(RegisterActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }
                        }
                    }).start();
                }

                }else{
                    Toast.makeText(RegisterActivity.this,"请将注册信息输入完整！",Toast.LENGTH_SHORT).show();
                }

    }

}